package com.group.activity.listener;

import com.group.activity.entity.ShopOrderEntity;
import com.group.activity.service.ShopOrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author lins
 * @date 2021/11/16 10:07
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
@Slf4j
public class SeckillCloseOrderListener {
    @Autowired
    ShopOrderService orderService;

    @RabbitHandler
    public void seckillCreateOrder(ShopOrderEntity entity, Channel channel, Message message) throws IOException {
        try {
            log.info("支付时间超时,关闭订单");
            //TODO 查询支付宝接口
            orderService.closeSeckillOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);
        }
    }
}
